from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import traceback
class Base:
    def __init__(self,driver):
        self.driver = driver
        self.loctors = {
            "xpath":By.XPATH,
            "class name":By.CLASS_NAME,
            "name":By.NAME,
            "link text":By.LINK_TEXT,
            "id":By.ID,
            "tag name":By.TAG_NAME,
            "css selector":By.CSS_SELECTOR,
            "partial link text":By.PARTIAL_LINK_TEXT
        }
    def find_element(self,loc,element):
        """
        :param loc:  定位方式
        :param element:  定位元素
        :return:
        """
        try:
            ele = WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((self.loctors[loc],element)))
            return ele
        except:
            traceback.print_exc()

    def find_elements(self,loc,element,num):
        """
        :param loc: 定位方式
        :param element:   定位元素
        :param num:  第几个
        :return:
        """
        try:
            ele = WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located((self.loctors[loc],element)))[num]
            return ele
        except:
            traceback.print_exc()

    def click_element(self,loc,element):
        try:
            self.find_element(loc,element).click()
        except:
            traceback.print_exc()

    def click_elements(self,loc,element,num):
        try:
            self.find_elements(loc,element,num).click()
        except:
            traceback.print_exc()

    def get_text(self,loc,element):
        text = self.find_element(loc,element).text
        return text

    def input_text(self,loc,element,text):
        """
        :param loc: 传入定位方式
        :param element: 传入定位元素
        :param text: 要输入的文本内容
        :return:
        """
        ele = self.find_element(loc,element)
        # 先清空
        ele.clear()
        # 再写入
        ele.send_keys(text)

    def input_texts(self,loc,element,num,text):
        ele = self.find_elements(loc,element,num)
        # 先清空
        ele.clear()
        # 再输入
        ele.send_keys(text)

    def win_handles(self,num):
        win = self.driver.window_handles
        self.driver.switch_to.window(win[num])



























